package cn.com.hhrcw.utils;

import cn.com.hhrcw.entity.*;
import cn.com.hhrcw.service.*;
import cn.com.hhrcw.sms.service.ISysSmsTemplateService;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @program: recruit-boot-parent
 * @description:
 * @author: JD
 * @create: 2020-11-27 20:00
 **/
@Component
public class EmailSendUtil {

    @Autowired
    private ISysSmsTemplateService sysSmsTemplateService;

    @Autowired
    private IPiInfoService piInfoService;

    @Autowired
    private IOpDeliveryHistoryService opDeliveryHistoryService;

    @Autowired
    private IOpJobService opJobService;

    @Autowired
    private IOpInterviewHistoryService opInterviewHistoryService;

    @Autowired
    private IEnInfoService enInfoService;

    @Autowired
    private IOpEntryHistoryService opEntryHistoryService;

    /**
     * @param receiveId   接收者id
     * @param sendId      发送者id
     * @param receiveType 接收者类型 0个人1企业
     * @param sendType    发送者类型 0个人1企业
     * @param objId       对应内容id
     * @param newsType    消息内容类型 类型0投递1面试2录用3拒绝面试4接受面试5阅读6接受入职7拒绝入职8简历不合适通知9面试通过10面试未通过11面试时间被修改
     * @return
     */
    public Map<String, String> creatEmail(String receiveId, String sendId, Integer receiveType, Integer sendType, String objId, String newsType) {
        PiInfo userState = piInfoService.lambdaQuery()
            .eq(PiInfo::getUserId, sendId)
            .eq(PiInfo::getDelFlag, false)
            .one();
        PiInfo receiveUser = piInfoService.lambdaQuery()
            .eq(PiInfo::getUserId, receiveId)
            .eq(PiInfo::getDelFlag, false)
            .one();
        String content = "";
        String tittle = "";
        Map<String, Object> map = new HashMap<>(5);
        boolean isMan = false;
        boolean isWoMan = false;
        if(receiveUser.getSex() != null){
            isMan = receiveUser.getSex() == 1 || receiveUser.getSex() == 0;
            isWoMan = receiveUser.getSex() == 2;
        }
        if (isMan) {
            map.put("sex", "先生");
        } else if (isWoMan) {
            map.put("sex", "女士");
        }
        String phone = userState.getPhone();
        userState.setPhone(phone.replace("@ent", "").replace("@admin", ""));
        int typeInt = Integer.parseInt(newsType);
        switch (typeInt) {
            case 0:
                this.deliveryNotice(map, objId);
                content = sysSmsTemplateService.sendNews("delivery_resume_email", map);
                tittle = sysSmsTemplateService.getTittle("delivery_resume_email");
                break;
            case 1:
                map.put("name", receiveUser.getName());
                OpInterviewHistory interviewHistory = opInterviewHistoryService.getById(objId);
                this.onlineInterviewNotic(map, objId);
                content = sysSmsTemplateService.sendNews("inview_notice_up_email", map);
                if(!map.containsKey("endTime") || map.get("endTime") == null || StrUtil.isBlank(map.get("endTime").toString())){
                    content = content.replace("——", "");
                }
                tittle = sysSmsTemplateService.getTittle("inview_notice_up_email");
                break;
            case 2:
                map.put("name", receiveUser.getName());
                this.entryInvitation(map, objId);
                content = sysSmsTemplateService.sendNews("entry_notice_email", map);
                tittle = sysSmsTemplateService.getTittle("entry_notice_email");
                break;
            case 3:
                this.interviewAcceptAndRejct(map, objId);
                content = sysSmsTemplateService.sendNews("inview_refuse_notice_email", map);
                tittle = sysSmsTemplateService.getTittle("inview_refuse_notice_email");
                break;
            case 4:
                this.interviewAcceptAndRejct(map, objId);
                content = sysSmsTemplateService.sendNews("inview_accept_notice_email", map);
                tittle = sysSmsTemplateService.getTittle("inview_accept_notice_email");
                break;
            case 5:
                map.put("name", receiveUser.getName());
                this.read(map, objId);
                content = sysSmsTemplateService.sendNews("read_notice_email", map);
                tittle = sysSmsTemplateService.getTittle("read_notice_email");
                break;
            case 6:
                this.acceptEntry(map, objId);
                content = sysSmsTemplateService.sendNews("entry_accept_notice_email", map);
                tittle = sysSmsTemplateService.getTittle("entry_accept_notice_email");
                break;
            case 7:
                this.acceptEntry(map, objId);
                content = sysSmsTemplateService.sendNews("entry_refuse_notice_email", map);
                tittle = sysSmsTemplateService.getTittle("entry_refuse_notice_email");
                break;
            case 8:
                this.reason(map, objId);
                content = sysSmsTemplateService.sendNews("resume_inappropriate_email", map);
                tittle = sysSmsTemplateService.getTittle("resume_inappropriate_email");
                break;
            case 9:
                map.put("name", receiveUser.getName());
                this.interviewPass(map, objId);
                content = sysSmsTemplateService.sendNews("inview_notice_pass_email", map);
                tittle = sysSmsTemplateService.getTittle("inview_notice_pass_email");
                break;
            case 10:
                map.put("name", receiveUser.getName());
                this.interviewPass(map, objId);
                content = sysSmsTemplateService.sendNews("inview_notice_no_pass_email", map);
                tittle = sysSmsTemplateService.getTittle("inview_notice_no_pass_email");
                break;
            case 11:
                map.put("name", receiveUser.getName());
                this.interviewPass(map, objId);
                content = sysSmsTemplateService.sendNews("update_time_notice_email", map);
                tittle = sysSmsTemplateService.getTittle("update_time_notice_email");
                break;
            case 12:
                map.put("phone", receiveUser.getPhone());
                map.put("name", receiveUser.getName());
                this.interviewPass(map, objId);
                content = sysSmsTemplateService.sendNews("interview_begin_notice_email", map);
                tittle = sysSmsTemplateService.getTittle("interview_begin_notice_email");
                break;
            case 13:
                map.put("phone", receiveUser.getPhone());
                map.put("name", receiveUser.getName());
                this.interviewPass(map, objId);
                content = sysSmsTemplateService.sendNews("interview_cover_notice_email", map);
                tittle = sysSmsTemplateService.getTittle("interview_cover_notice_email");
                break;
            default:
                break;
        }
        Map<String, String> res = new LinkedHashMap<>();
        res.put("content", content);
        res.put("tittle", tittle);
        return res;
    }

    /**
     * 投递通知
     *
     * @param map
     * @param objId
     */
    void deliveryNotice(Map<String, Object> map, String objId) {
        OpDeliveryHistory deliveryHistory = opDeliveryHistoryService.getById(objId);
        String jobId = deliveryHistory.getJobId();
        OpJob byId = opJobService.getById(jobId);
        map.put("createTime", DateUtil.format(deliveryHistory.getCreateTime(), DatePattern.NORM_DATETIME_PATTERN));
        map.put("jobName", byId.getJobName());
    }

    /**
     * 在线面试通知 线下面试通知
     *
     * @param map
     * @param objId
     */
    void onlineInterviewNotic(Map<String, Object> map, String objId) {
        OpInterviewHistory interviewHistory = opInterviewHistoryService.getById(objId);
        map.put("beginTime", DateUtil.format(interviewHistory.getTime(), DatePattern.NORM_DATETIME_PATTERN));
        map.put("endTime", DateUtil.format(interviewHistory.getInterviewEndTime(), DatePattern.NORM_DATETIME_PATTERN));
        String companyId = interviewHistory.getCompanyId();
        EnInfo enInfo = enInfoService.getById(companyId);
        map.put("enName", enInfo.getEnName());
    }

    /**
     * 入职邀请
     *
     * @param map
     * @param objId
     */
    void entryInvitation(Map<String, Object> map, String objId) {
        OpEntryHistory entryHistory = opEntryHistoryService.getById(objId);
        map.put("time", DateUtil.format(entryHistory.getTime(), DatePattern.NORM_DATETIME_PATTERN));
        String companyId = entryHistory.getCompanyId();
        EnInfo enInfo = enInfoService.getById(companyId);
        map.put("address", enInfo.getEnLocation());
        map.put("enName", enInfo.getEnName());
    }

    /**
     * 面试邀请被拒绝 / 接受面试
     *
     * @param map
     * @param objId
     */
    void interviewAcceptAndRejct(Map<String, Object> map, String objId) {
        OpInterviewHistory interviewHistory = opInterviewHistoryService.getById(objId);
        String jobId = interviewHistory.getJobId();
        OpJob byId = opJobService.getById(jobId);
        map.put("time", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        map.put("jobName", byId.getJobName());
        EnInfo enInfo = enInfoService.getById(byId.getCompanyId());
        map.put("enName", enInfo.getEnName());
    }

    /**
     * 阅读
     *
     * @param map
     * @param objId
     */
    void read(Map<String, Object> map, String objId) {
        OpDeliveryHistory deliveryHistory = opDeliveryHistoryService.getById(objId);
        String companyId = deliveryHistory.getCompanyId();
        EnInfo enInfo = enInfoService.getById(companyId);
        map.put("enName", enInfo.getEnName());
    }

    /**
     * 接受入职
     *
     * @param map
     * @param objId
     */
    void acceptEntry(Map<String, Object> map, String objId) {
        OpEntryHistory entryHistory = opEntryHistoryService.getById(objId);
        String jobId = entryHistory.getJobId();
        OpJob byId = opJobService.getById(jobId);
        EnInfo enInfo = enInfoService.getById(byId.getCompanyId());
        map.put("enName", enInfo.getEnName());
        map.put("time", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        map.put("jobName", byId.getJobName());
    }

    /**
     * 面试结果
     *
     * @param map
     * @param objId
     */
    void reason(Map<String, Object> map, String objId) {
        OpDeliveryHistory deliveryHistory = opDeliveryHistoryService.getById(objId);
        String info = deliveryHistory.getInappropriateInfo();
        if (StrUtil.isNotBlank(info)) {
            switch (info) {
                case "0":
                    map.put("reason", "工作经验不满足要求");
                    break;
                case "1":
                    map.put("reason", "技术经验不符合岗位要求");
                    break;
                case "2":
                    map.put("reason", "月薪不匹配");
                    break;
                case "3":
                    map.put("reason", "学历不匹配");
                    break;
                case "4":
                    map.put("reason", "行业不匹配");
                    break;
                case "5":
                    map.put("reason", "招聘类型不匹配");
                    break;
                default:
                    map.put("reason", "无");
                    break;
            }
        }
        String jobId = deliveryHistory.getJobId();
        OpJob byId = opJobService.getById(jobId);
        map.put("jobName", byId.getJobName());
        EnInfo enInfo = enInfoService.getById(byId.getCompanyId());
        map.put("enName", enInfo.getEnName());
    }

    /**
     * 面试通过通知
     *
     * @param map
     * @param objId
     */
    void interviewPass(Map<String, Object> map, String objId) {
        OpInterviewHistory interviewHistory = opInterviewHistoryService.getById(objId);
        String jobId = interviewHistory.getJobId();
        OpJob byId = opJobService.getById(jobId);
        map.put("jobName", byId.getJobName());
        String companyId = byId.getCompanyId();
        EnInfo enInfo = enInfoService.getById(companyId);
        map.put("enName", enInfo.getEnName());
        map.put("time", DateUtil.format(interviewHistory.getTime(), DatePattern.NORM_DATETIME_PATTERN));
    }
}
